﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using KLY.ServiceCore;
using KLY.DataModels;
using KLY.Tools.Helper;

namespace KLY_Webs.Controllers.ApiControl
{
    public class AccountApiController : ApiController
    {
        [HttpPost]
         public string GetVerifyCode(SendMessageRequest request)
        {
            var processer = new GerVerifyCode(request);
            var response = processer.Process();
            return JsonHelper.Serialize(response);
        }

        [HttpPost]
        public string RegisterAccount(RegisterReuest request)
        {
            var processer = new RegisterAccount(request);
            var response = processer.Process();
            BizCommon.SetLoginInfo(response.Result.token,
                        string.IsNullOrWhiteSpace(response.Result.Nick_Name) ? request.Phone : response.Result.Nick_Name,
                        response.Result.Member_Type.ToString());
            return JsonHelper.Serialize(response);
        }

        [HttpPost]
        public string GetInviterName(GetInviterRequest request)
        {
            var processer = new GetInviterName(request);
            var response = processer.Process();
            return JsonHelper.Serialize(response);
        }

        [HttpPost]
        public string checkLogin()
        {
            var processeer = new CheckLogin();
            var response = processeer.Process();
            if (response != null && response.Ok)
            {
                var loginInfo = BizCommon.GetLoginInfo();
                if (loginInfo != null && loginInfo.Count >= 3)
                {
                    return "{\"Ok\":true,\"account\":" + loginInfo[1] + ",\"membertype\":\"" + BizCommon.GetMemberTypeDesc(loginInfo[2]) + "\"}";
                }
                    
            }
            return JsonHelper.Serialize(response);
        }

        [HttpPost]
        public string Logout()
        {
            var processeer = new Logout();
            var response = processeer.Process();
            if (response != null && response.Ok)
            {
                BizCommon.DeleteLoginInfo();
            }
            return JsonHelper.Serialize(response);
        }
    }
}
